/********************************************************************************
 Platform: SPARK V
 Experiment: 1_Buzzer_Beep
 Written by: Vinod Desai, NEX Robotics Pvt. Ltd.
 Edited By: Sachitanand Malewar, NEX Robotics Pvt. Ltd.
 Last Modification: 22nd September 2011
 AVR Studio Version 4.17, Build 666

 Concepts covered: Buzzer interfacing

 This program will make Board buzzer beep. Buzzer is connected at the third pin 
 of the PORTC. To turn on the buzzer, make PORTC3 pin logic 1.
 
 For more detail on hardware and theory refer the hardware manual. 

 Note:
 
 1. Make sure that in the configuration options following settings are 
 	done for proper operation of the code

 	Microcontroller: atmega16
 	Frequency: 7372800Hz
 	Optimization: -O0 (For more information read section: Selecting proper 
	              optimization options on page no.16 in the manual)
 
*********************************************************************************/


#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

//Function to initialize Buzzer 
void buzzer_pin_config (void)
{
 DDRC = DDRC | 0x08;		//Setting PORTC 3 as output
 PORTC = PORTC & 0xF7;		//Setting PORTC 3 logic low to turnoff buzzer
}

void port_init (void)
{
 buzzer_pin_config();
}

void buzzer_on (void)
{
 unsigned char port_restore = 0;
 port_restore = PINC;
 port_restore = port_restore | 0x08;
 PORTC = port_restore;
}

void buzzer_off (void)
{
 unsigned char port_restore = 0;
 port_restore = PINC;
 port_restore = port_restore & 0xF7;
 PORTC = port_restore;
}

void init_devices (void)
{
 cli(); 			//Clears the global interrupts
 port_init();
 sei(); 			//Enables the global interrupts
}

//Main Function
int main(void)
{
	init_devices();
	while(1)
	{
		buzzer_on();
		_delay_ms(1000);		//delay
		buzzer_off();
		_delay_ms(1000);		//delay
	}
}


